package com.bianyuanren.web.controller.app;

import com.qiniu.util.Auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 微笑的你
 * @version 1.0
 * @date 2020/5/28 0028 上午 11:11
 */
@Controller
public class UploadController {
    // ******的内容需要查看七牛云账号的相关信息
    private static final String accessKey = "Oy63b47SsWt-2-CT5PLLhKl3YuweeuV4PJhpL0KS";    //访问秘钥
    private static final String secretKey = "dov6bhSYXXMGP0sxplLVTjwTy7dZYBeRUKbIsPVa";    //授权秘钥
    private static final String bucket = "pinsgle";       //存储空间名称
    private static final String domain = "http://qiniu.huarenair.com/";       //外链域名

//    // 设置需要操作的账号的AK和SK
//    private static final String ACCESS_KEY = "Oy63b47SsWt-2-CT5PLLhKl3YuweeuV4PJhpL0KS";
//    private static final String SECRET_KEY = "dov6bhSYXXMGP0sxplLVTjwTy7dZYBeRUKbIsPVa";
//    // 要上传的空间
//    private static final String bucketname ="pinsgle";
//    // 密钥
//    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//    private static final String DOMAIN = "http://qiniu.huarenair.com/";


    /**
     * 七牛云上传生成凭证
     *
     * @throws Exception
     */
    @RequestMapping("/QiniuUpToken")
    @ResponseBody
    @CrossOrigin(value = "*")
    public Map<String, Object> QiniuUpToken(@RequestParam String suffix) throws Exception{
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //验证七牛云身份是否通过
            Auth auth = Auth.create(accessKey, secretKey);
            //生成凭证
            String upToken = auth.uploadToken(bucket);
            result.put("token", upToken);
            //存入外链默认域名，用于拼接完整的资源外链路径
            result.put("domain", domain);


            //生成实际路径名
            String randomFileName = UUID.randomUUID().toString() + suffix;
            result.put("imgUrl", randomFileName);
            result.put("success", 1);
        } catch (Exception e) {
            result.put("message", "获取凭证失败，"+e.getMessage());
            result.put("success", 0);
        } finally {
            return result;
        }
    }

    public static void main(String[] args) {

    }
}
